/*
 * Copyright (C) Cátedra SAES-UMU 2011 <pedromateo@um.es>
 *
 * Android HCI Extractor is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Android HCI Extractor is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.mmi.android.instrumentation.viewproxies;

import java.lang.reflect.Field;
import java.util.LinkedList;

import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

/**
 * Base for view-proxy classes.
 * These classes are used to easily get some common
 * information from different objects with a common
 * super-class.
 * 
 * @author pedro mateo (pedromateo@um.es)
 *
 */
public abstract class BaseViewProxy {

	/**
	 * Constructor.
	 */
	public BaseViewProxy(){
	}

	///
	/// abstract methods
	///

	/**
	 * Returns the number of elements included in this view.
	 * @param v the view
	 * @return elements
	 */
	public abstract int getElements(View v);

	/**
	 * Returns true if the view represents a question.
	 * @param v the view
	 * @return is question
	 */
	public abstract boolean isQuestion(View v);

	/**
	 * Returns the number of concepts included in this view.
	 * @param v the view
	 * @return elements
	 */
	public abstract int getConcepts(View v);

	/**
	 * Returns the number of noise elements included in this view.
	 * @param v the view
	 * @return elements
	 */
	public abstract int getNoise(View v);

	/**
	 * Returns true if the view represents system feedback.
	 * @param v the view
	 * @return is feedback
	 */
	public abstract boolean isFeedback(View v);

	/**
	 * List of classes adapted by this proxy.
	 */
	public abstract LinkedList<Class> getAdaptedClasses();

	///
	/// variables
	///

	protected View view_;

	///
	/// static methods
	///

	/**
	 * Returns the OnClickListener of a view.
	 * @param v the view object
	 * @return the listener
	 */
	public static OnClickListener getOnClickListener(View v){
		try {
			Class cls = View.class;//t.getClass();
	        Field f = cls.getDeclaredField("mOnClickListener");
	        f.setAccessible(true);
	        //System.out.println(f.toString());
	        return (OnClickListener) f.get(v);
		} catch (Exception e) {
			//e.printStackTrace();
			return null;
		}
	}

	/**
	 * Returns the OnTouchListener of a view.
	 * @param v the view object
	 * @return the listener
	 */
	public static OnTouchListener getOnTouchListener(View v){
		try {
			Field f = View.class.getDeclaredField("mOnTouchListener");
			f.setAccessible(true);
			return (OnTouchListener)f.get(v);
		} catch (Exception e) {
			//e.printStackTrace();	
			return null;
		}
	}

	/**
	 * Returns the OnLongClickListener of a view.
	 * @param v the view object
	 * @return the listener
	 */
	public static OnLongClickListener getOnLongClickListener(View v){
		try {
			Field f = View.class.getDeclaredField("mOnLongClickListener");
			f.setAccessible(true);
			return (OnLongClickListener)f.get(v);
		} catch (Exception e) {
			//e.printStackTrace();	
			return null;
		}
	}

	/**
	 * Returns the OnItemClickListener of a view.
	 * @param v the view object
	 * @return the listener
	 */
	public static OnItemClickListener getOnItemClickListener(View v){
		try {
			Field f = View.class.getDeclaredField("mOnItemClickListener");
			f.setAccessible(true);
			return (OnItemClickListener)f.get(v);
		} catch (Exception e) {
			//e.printStackTrace();	
			return null;
		}
	}
	
	/**
	 * Returns the OnItemLongClickListener of a view.
	 * @param v the view object
	 * @return the listener
	 */
	public static OnItemLongClickListener getOnItemLongClickListener(View v){
		try {
			Field f = View.class.getDeclaredField("mOnItemLongClickListener");
			f.setAccessible(true);
			return (OnItemLongClickListener)f.get(v);
		} catch (Exception e) {
			//e.printStackTrace();	
			return null;
		}
	}

	/**
	 * Returns the OnKeyboardActionListener of a view.
	 * @param v the view object
	 * @return the listener
	 */
	public static OnKeyboardActionListener getOnKeyboardActionListener(View v){
		try {
			Field f = KeyboardView.class.getDeclaredField("mOnKeyboardActionListener");
			f.setAccessible(true);
			return (OnKeyboardActionListener)f.get((KeyboardView)v);
		} catch (Exception e) {
			//e.printStackTrace();
			return null;	
		}
	}
	
	/**
	 * Returns the OnKeyListener of a view.
	 * @param v the view object
	 * @return the listener
	 */
	public static OnKeyListener getOnKeyListener(View v){
		try {
	        Field f = View.class.getDeclaredField("mOnKeyListener");
	        f.setAccessible(true);
	        //System.out.println(f.toString());
	        return (OnKeyListener) f.get(v);
		} catch (Exception e) {
			//e.printStackTrace();
			return null;
		}
	}
}
